<template>
	<view class="sp-page sp-user__create">
		<view class="sp-user__create-item">
			<label>账号</label>
			<input v-model="form.username" placeholder="请输入用户名" type="text">
		</view>
		<view class="sp-user__create-item">
			<label>密码</label>
			<input v-model="form.password" placeholder="请输入密码" type="password">
		</view>
		<view class="sp-user__create-check" @click="() => { form.is_manager = form.is_manager == 0 ? 1 : 0 }">
			<view class="sp-user__create-checked" :class="{ 'active': form.is_manager == 1 }"></view>
			<text>是否管理员</text>
		</view>
		<view class="sp-user__create-check" @click="() => { form.is_return = form.is_return == 0 ? 1 : 0 }">
			<view class="sp-user__create-checked" :class="{ 'active': form.is_return == 1 }"></view>
			<text>是否内部人员</text>
		</view>
		<view class="sp-room__submit">
			<button @click="setUser">保存</button>
		</view>
	</view>
</template>

<script>
	import { createUser } from '@/api/task.js'
	
	export default {
		name: 'spCreateForm',
		data() {
			return {
				form: {
					username: '',
					password: '',
					is_manager: 0,
					is_return: 0
				}
			}
		},
		mounted() {},
		methods: {
			setUser() {
				if (this.form.username === '' && this.form.username < 5) {
					this.$u.toast('用户名不能为空且需大于5位')
					return false
				}
				if (this.form.password === '') {
					this.$u.toast('密码不能为空')
					return false
				}
				const params = {
					...this.form
				}
				createUser(params).then(res => {
					if (res && res.username) {
						this.form.username = ''
						this.form.password = ''
						this.$u.toast('创建成功')
					} else {
						this.$u.toast('创建失败')
					}
				}).catch(err => {
					this.$u.toast('创建失败')
				})
			}
		}
	}
</script>

<style lang="scss">
	.sp-user__create {
		padding: 0 20rpx;
		background: #fff;
		
		&- {
			
			&check {
				display: flex;
				align-items: center;
				padding: 20rpx 0;
				
				text {
					font-size: 24rpx;
					color: #666;
				}
			}
			
			&checked {
				width: 32rpx;
				height: 32rpx;
				border: 1px solid #ccc;
				margin-right: 20rpx;
				transition: all .3s;
				
				&.active {
					background: url('../../static/images/user/checked.png') center no-repeat;
					background-size: 80% auto;
				}
			}
			
			&item {
				padding-bottom: 20rpx;
				border-bottom: 1rpx solid #eee;
				
				label {
					display: block;
					font-size: 24rpx;
					color: #666;
					padding: 20rpx 0;
				}
				
				input {
					border: 0;
					height: 66rpx;
					font-size: 28rpx;
				}
			}
		}
	}
</style>
